package com.gdpu.project.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.gdpu.page.vo.QueryResult;
import com.gdpu.project.dao.ProjectDao;
import com.gdpu.project.vo.Project;

/**
 * A data access object (DAO) providing persistence and search support for
 * Project entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.gdpu.project.vo.Project
 * @author MyEclipse Persistence Tools
 */

public class ProjectDaoImpl extends HibernateDaoSupport implements ProjectDao {
	private static final Log log = LogFactory.getLog(ProjectDaoImpl.class);
	// property constants
	public static final String PROJECT_ID = "projectId";
	public static final String PROJECT_TYPE = "projectType";
	public static final String PROJECT_NAME = "projectName";
	public static final String PROJECT_ORGIN = "projectOrgin";
	public static final String PROJECT_XZ = "projectXz";
	public static final String CYRS = "cyrs";
	public static final String COMMENT = "comment";
	public static final String PROJECT_FILE = "projectFile";
	public static final String FILE_PATH = "filePath";
	public static final String APPLY_STATE = "applyState";
	public static final String APPAL_BZ = "appalBz";
	public static final String APPROVAL_STATE = "approvalState";
	public static final String APPROVAL_BZ = "approvalBz";
	public static final String APPROVAL_FILE_NAME = "approvalFileName";
	public static final String APPROVAL_FILE_PATH = "approvalFilePath";
	public static final String MONITOR_STATE = "monitorState";
	public static final String MONITOR_BZ = "monitorBz";
	public static final String MONITOR_FILE_NAME = "monitorFileName";
	public static final String MONITOR_FILE_PATH = "monitorFilePath";
	public static final String END_STATE = "endState";
	public static final String END_BZ = "endBz";
	public static final String END_FILE_NAME = "endFileName";
	public static final String END_FILE_PATH = "endFilePath";
	public static final String TEACHER_ID = "teacherId";
	public static final String TEACHER_NAME = "teacherName";

	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#save(com.gdpu.project.vo.Project)
	 */
	public void save(Project transientInstance) {
		log.debug("saving Project instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#delete(com.gdpu.project.vo.Project)
	 */
	public void delete(Project persistentInstance) {
		log.debug("deleting Project instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findById(java.lang.Integer)
	 */
	public Project findById(java.lang.Integer id) {
		log.debug("getting Project instance with id: " + id);
		try {
			Project instance = (Project) getHibernateTemplate().get(
					"com.gdpu.project.vo.Project", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByExample(com.gdpu.project.vo.Project)
	 */
	public List<Project> findByExample(Project instance) {
		log.debug("finding Project instance by example");
		try {
			List<Project> results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByProperty(java.lang.String, java.lang.Object)
	 */
	public QueryResult<Project> findByProperty(String propertyName, Object value,int firstindex,int maxresult) {
		log.debug("finding Project instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Project as model";
			StringBuffer queryBuffer=new StringBuffer();
			queryBuffer.append(queryString);
			Query query=null;
			if(propertyName!=null&&value!=null){
				queryBuffer.append(" where model."+ propertyName + "= :propertyName");
			}
			query=getSession().createQuery(queryBuffer.toString());
			if(queryBuffer.indexOf(" where model."+ propertyName + "= :propertyName")!=-1){
				query.setParameter("propertyName", value);
			}
			if(firstindex!=-1&&maxresult!=-1){
				query.setFirstResult(firstindex).setMaxResults(maxresult);
			}
			QueryResult<Project> queryResult=new QueryResult<Project>();
			queryResult.setResultlist(query.list());
			queryBuffer.delete(0, queryBuffer.length());
			queryBuffer.append("select count(*) from Project model");
			if(propertyName!=null&&value!=null){
				queryBuffer.append(" where model."+ propertyName + "= :propertyName");
			}
			query=getSession().createQuery(queryBuffer.toString());
			if(queryBuffer.indexOf(" where model."+ propertyName + "= :propertyName")!=-1){
				query.setParameter("propertyName", value);
			}
			queryResult.setTotalrecord((Long)query.uniqueResult());
			return queryResult;
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByProjectId(java.lang.Object)
	 */
	public QueryResult<Project> findByProjectId(Object projectId,int firstindex,int maxresult) {
		return findByProperty(PROJECT_ID, projectId, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByProjectType(java.lang.Object)
	 */
	public QueryResult<Project> findByProjectType(Object projectType,int firstindex,int maxresult) {
		return findByProperty(PROJECT_TYPE, projectType, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByProjectName(java.lang.Object)
	 */
	public QueryResult<Project> findByProjectName(Object projectName,int firstindex,int maxresult) {
		return findByProperty(PROJECT_NAME, projectName, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByProjectOrgin(java.lang.Object)
	 */
	public QueryResult<Project> findByProjectOrgin(Object projectOrgin,int firstindex,int maxresult) {
		return findByProperty(PROJECT_ORGIN, projectOrgin, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByProjectXz(java.lang.Object)
	 */
	public QueryResult<Project> findByProjectXz(Object projectXz,int firstindex,int maxresult) {
		return findByProperty(PROJECT_XZ, projectXz, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByCyrs(java.lang.Object)
	 */
	public QueryResult<Project> findByCyrs(Object cyrs,int firstindex,int maxresult) {
		return findByProperty(CYRS, cyrs, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByComment(java.lang.Object)
	 */
	public QueryResult<Project> findByComment(Object comment,int firstindex,int maxresult) {
		return findByProperty(COMMENT, comment, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByProjectFile(java.lang.Object)
	 */
	public QueryResult<Project> findByProjectFile(Object projectFile,int firstindex,int maxresult) {
		return findByProperty(PROJECT_FILE, projectFile, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByFilePath(java.lang.Object)
	 */
	public QueryResult<Project> findByFilePath(Object filePath,int firstindex,int maxresult) {
		return findByProperty(FILE_PATH, filePath, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByApplyState(java.lang.Object)
	 */
	public QueryResult<Project> findByApplyState(Object applyState,int firstindex,int maxresult) {
		return findByProperty(APPLY_STATE, applyState, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByAppalBz(java.lang.Object)
	 */
	public QueryResult<Project> findByAppalBz(Object appalBz,int firstindex,int maxresult) {
		return findByProperty(APPAL_BZ, appalBz, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByApprovalState(java.lang.Object)
	 */
	public QueryResult<Project> findByApprovalState(Object approvalState,int firstindex,int maxresult) {
		return findByProperty(APPROVAL_STATE, approvalState, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByApprovalBz(java.lang.Object)
	 */
	public QueryResult<Project> findByApprovalBz(Object approvalBz,int firstindex,int maxresult) {
		return findByProperty(APPROVAL_BZ, approvalBz, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByApprovalFileName(java.lang.Object)
	 */
	public QueryResult<Project> findByApprovalFileName(Object approvalFileName,int firstindex,int maxresult) {
		return findByProperty(APPROVAL_FILE_NAME, approvalFileName, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByApprovalFilePath(java.lang.Object)
	 */
	public QueryResult<Project> findByApprovalFilePath(Object approvalFilePath,int firstindex,int maxresult) {
		return findByProperty(APPROVAL_FILE_PATH, approvalFilePath, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByMonitorState(java.lang.Object)
	 */
	public QueryResult<Project> findByMonitorState(Object monitorState,int firstindex,int maxresult) {
		return findByProperty(MONITOR_STATE, monitorState, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByMonitorBz(java.lang.Object)
	 */
	public QueryResult<Project> findByMonitorBz(Object monitorBz,int firstindex,int maxresult) {
		return findByProperty(MONITOR_BZ, monitorBz, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByMonitorFileName(java.lang.Object)
	 */
	public QueryResult<Project> findByMonitorFileName(Object monitorFileName,int firstindex,int maxresult) {
		return findByProperty(MONITOR_FILE_NAME, monitorFileName, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByMonitorFilePath(java.lang.Object)
	 */
	public QueryResult<Project> findByMonitorFilePath(Object monitorFilePath,int firstindex,int maxresult) {
		return findByProperty(MONITOR_FILE_PATH, monitorFilePath, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByEndState(java.lang.Object)
	 */
	public QueryResult<Project> findByEndState(Object endState,int firstindex,int maxresult) {
		return findByProperty(END_STATE, endState, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByEndBz(java.lang.Object)
	 */
	public QueryResult<Project> findByEndBz(Object endBz,int firstindex,int maxresult) {
		return findByProperty(END_BZ, endBz, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByEndFileName(java.lang.Object)
	 */
	public QueryResult<Project> findByEndFileName(Object endFileName,int firstindex,int maxresult) {
		return findByProperty(END_FILE_NAME, endFileName, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByEndFilePath(java.lang.Object)
	 */
	public QueryResult<Project> findByEndFilePath(Object endFilePath,int firstindex,int maxresult) {
		return findByProperty(END_FILE_PATH, endFilePath, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByTeacherId(java.lang.Object)
	 */
	public QueryResult<Project> findByTeacherId(Object teacherId,int firstindex,int maxresult) {
		return findByProperty(TEACHER_ID, teacherId, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findByTeacherName(java.lang.Object)
	 */
	public QueryResult<Project> findByTeacherName(Object teacherName,int firstindex,int maxresult) {
		return findByProperty(TEACHER_NAME, teacherName, firstindex, maxresult);
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#findAll()
	 */
	public List<Project> findAll() {
		log.debug("finding all Project instances");
		try {
			String queryString = "from Project";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#merge(com.gdpu.project.vo.Project)
	 */
	public Project merge(Project detachedInstance) {
		log.debug("merging Project instance");
		try {
			Project result = (Project) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#attachDirty(com.gdpu.project.vo.Project)
	 */
	public void attachDirty(Project instance) {
		log.debug("attaching dirty Project instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.gdpu.project.vo.ProjectDao#attachClean(com.gdpu.project.vo.Project)
	 */
	public void attachClean(Project instance) {
		log.debug("attaching clean Project instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ProjectDao getFromApplicationContext(ApplicationContext ctx) {
		return (ProjectDao) ctx.getBean("ProjectDAO");
	}
	
}